//
//  ViewController.m
//  UI09_UITableViewEdit
//
//  Created by Hanlingyu on 16/7/26.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
<
    UITableViewDataSource,
    UITableViewDelegate
>
@property (nonatomic, strong) NSMutableArray *nameArray;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray<NSIndexPath *> *selectIndexArray;
@property (nonatomic, strong) NSMutableArray<NSString *> *selectNameArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.selectIndexArray = [NSMutableArray array];
    self.selectNameArray = [NSMutableArray array];
    self.view.backgroundColor = [UIColor lightGrayColor];
    NSArray *array = @[@"唐益梦", @"张合尉", @"高雅馨", @"符淋淋", @"孙遥", @"李毅然", @"于瀚", @"沙长健", @"卢思彤", @"徐昊", @"曹冠雄", @"陈丹明", @"罗浩", @"肖扬", @"王道林", @"宫雪", @"李成", @"冀永金", @"姜浩", @"罗鹏飞", @"李昊林", @"郭宝", @"马峰", @"刘坦奇", @"殷明静", @"邓凯", @"徐康", @"王鍠", @"韩旭", @"杨睿", @"张信宝", @"姜程程", @"包铭礼", @"杜江", @"苗钧豪"];
    self.nameArray = [NSMutableArray arrayWithArray:array];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
    // tableView的编辑
    // 1. 设置右barButtonItem 为编辑的deitButtonItem
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    NSLog(@"%@", NSStringFromSelector(self.editButtonItem.action));
    self.editButtonItem.action = @selector(editAction);
    
}
- (void)editAction {
    // 只要能拿到方法的方法名, 并且在这个对象里有对应的实现, 就可以调用
    [self performSelector:NSSelectorFromString(@"_toggleEditing:")];
    if (self.editing == NO) {
        
        
    }
    
}

#pragma mark - TableView Delegate
// 4. 返回cell的编辑状态(样式), 默认为Delete
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // UITableViewCellEditingStyleNone,
    // UITableViewCellEditingStyleDelete,
    // UITableViewCellEditingStyleInsert
    // 返回 UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert 对选
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.editing) {
        
        // 取出对应名字的下标
        NSString *name = _nameArray[indexPath.row];
        // 添加到险种名字的数组中
        [_selectNameArray addObject:name];
        // 在全选状态下, 数组中添加对应选中的下标
        [_selectIndexArray addObject:indexPath];
        
    }
    
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    if (self.editing) {
        
        // 在全选状态下, 数组中移除对应选中的下标
        [_selectIndexArray removeObjectAtIndex:indexPath.row];
        
    }
    
}


#pragma mark - TableView DataSource

// 2. 让tableView进入编辑状态
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
    if (editing) {
        NSLog(@"编辑");
    } else {
        NSLog(@"完成");
        // 要删除选中的数组

//        NSSet *set = [NSSet setWithArray:_selectIndexArray];
//        [self.nameArray removeObjectsAtIndexes:<#(nonnull NSIndexSet *)#>];
        [_nameArray removeObjectsInArray:_selectNameArray];
        [_tableView deleteRowsAtIndexPaths:_selectIndexArray withRowAnimation:UITableViewRowAnimationLeft];
        [_selectNameArray removeAllObjects];
        [_selectIndexArray removeAllObjects];
        // 删除cell
        
    }
    
}

// 3. 控制talbeView能否进行编辑, NO不能, YES能
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
// 5. 对应按钮的点击方法
// 滑动删除, 只和这个方法有关系
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSLog(@"删除");
#warning 如果对tableView进行添加 / 删除, 先修改数据源, 再去进行对应的添加 / 删除
        // 1. 修改数据源
        [self.nameArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        NSLog(@"插入");
        [_nameArray insertObject:@"王煌" atIndex:indexPath.row];
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
    }
    // tableView 不论删除还是添加, 都已经进行了刷新操作
}

// Move
// 1. 能否移动 默认 可以移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
// 2. 更改数据源
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {

    [_nameArray exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
    NSLog(@"%@", _nameArray);
    
}


// DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _nameArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    
    cell.textLabel.text = _nameArray[indexPath.row];
    
    return cell;
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
